/*
* Mentioned DB1 as your Compared Database here
*/
;WITH 
SrcObj AS (
              SELECT o.Name AS ObjName, o.Object_id
              FROM   mainserver.DB1.sys.Objects o
          ),
SrcCols AS (
               SELECT c.Object_id, o.Name AS TableName, c.Column_id, C.Name AS ColName
               FROM   mainserver.DB1.sys.Columns c
                      INNER JOIN mainserver.DB1.sys.Tables o
                           ON  o.OBJECT_ID = c.OBJECT_ID
           ),
SrcFks AS (
              SELECT fk.Name, Parent.TableName, Parent.ColName, Ref.TableName AS RefTableNAme, Ref.ColName AS RefColName, 
              Fk.delete_referential_action_desc COLLATE DATABASE_DEFAULT AS ONDelete
              FROM   mainserver.DB1.sys.foreign_keys Fk
                     INNER JOIN mainserver.DB1.sys.foreign_key_Columns Fc
                          ON  Fc.constraint_object_id = Fk.object_id
                     INNER JOIN SrcCols Parent
                          ON  Parent.OBJECT_ID = fc.parent_object_id AND Parent.column_id = FC.parent_column_id
                     INNER JOIN SrcCols Ref
                          ON  Ref.OBJECT_ID = fc.referenced_object_id  AND Ref.column_id = FC.referenced_column_id
          ),
DesCols AS (
               SELECT c.Object_id, o.Name AS TableName, c.Column_id, C.Name AS ColName
               FROM   sys.Columns c 
                      INNER JOIN sys.Tables o
                           ON  o.OBJECT_ID = c.OBJECT_ID
           ),
DesFks AS(
             SELECT fk.Name, Parent.TableName, Parent.ColName, Ref.TableName AS RefTableNAme, Ref.ColName AS RefColName, Fk.delete_referential_action_desc AS ONDelete
              FROM   sys.foreign_keys Fk
                     INNER JOIN sys.foreign_key_Columns Fc
                          ON  Fc.constraint_object_id = Fk.object_id
                     INNER JOIN DesCols Parent
                          ON  Parent.OBJECT_ID = fc.parent_object_id AND Parent.column_id = FC.parent_column_id
                     INNER JOIN DesCols Ref
                          ON  Ref.OBJECT_ID = fc.referenced_object_id  AND Ref.column_id = FC.referenced_column_id
         )	 
SELECT 
	   --s.name,s.TableName,S.colName,s.RefTableNAme,s.RefColName,
	   --d.name,d.TableName,d.colName,d.RefTableNAme,D.RefColName,
CreateStmt = 
'IF OBJECT_ID(''' + s.NAme + ''') IS NULL
ALTER TABLE [dbo].[' + s.TableName + '] WITH NOCHECK 
	ADD CONSTRAINT [' + s.Name+'] FOREIGN KEY([' + s.ColName +']) REFERENCES [dbo].[' +s.RefTableName + ']([' +s.RefcolName + ']) 
	ON DELETE ' + REPLACE(s.ONDelete,'_',' ')  + ';
Go '
FROM   srcFks s
       LEFT OUTER JOIN DesFks d
            ON s.name= d.name
WHERE  d.tablename IS NULL AND EXISTS (SELECT 1 FROM descols dc WHERE dc.colname =s.colname AND dc.Tablename =s.Tablename )
AND EXISTS (SELECT 1 FROM descols dc WHERE dc.colname =s.refcolname AND dc.Tablename =s.REfTableName )
 --s.Tablename ='AdDashRoleProfile' AND 
  


/*
SELECT DropStmt = 'IF OBJECT_ID(' + ForeignKeys.ForeignKeyName +
       ') IS NOT NULL 
					ALTER TABLE [' + ForeignKeys.ForeignTableSchema +
       '].[' + ForeignKeys.ForeignTableName +
       '] DROP CONSTRAINT [' + ForeignKeys.ForeignKeyName + ']; ', 
       CreateStmt = 'ALTER TABLE [' +
       ForeignKeys.ForeignTableSchema +
       '].[' + ForeignKeys.ForeignTableName +
       '] WITH NOCHECK ADD CONSTRAINT [' + ForeignKeys.ForeignKeyName +
       '] FOREIGN KEY([' + ForeignKeys.ForeignTableColumn +
       ']) REFERENCES [' + SCHEMA_NAME (sys.objects.schema_id) + '].[' +
       sys.objects.[name] + ']([' +
       sys.columns.[name] + ']) ON DELETE CASCADE; '
FROM   sys.objects
       INNER JOIN sys.columns
            ON  (sys.columns.[object_id] = sys.objects.[object_id])
       INNER JOIN (
                SELECT fk.[name] AS ForeignKeyName, SCHEMA_NAME (sys.objects.schema_id) AS 
                       ForeignTableSchema, sys.objects.[name] AS ForeignTableName, sys.columns.[name] AS 
                       ForeignTableColumn, fk.referenced_object_id AS referenced_object_id, sys.foreign_key_columns.referenced_column_id AS 
                       referenced_column_id
                FROM   sys.foreign_keys fk
                       INNER JOIN sys.foreign_key_columns
                            ON  (
                                    sys.foreign_key_columns.constraint_object_id = fk.[object_id]
                                )
                       INNER JOIN sys.objects
                            ON  (sys.objects.[object_id] = fk.parent_object_id)
                       INNER JOIN sys.columns
                            ON  (sys.columns.[object_id] = sys.objects.[object_id]) AND (
                                    sys.columns.column_id = sys.foreign_key_columns.parent_column_id
                                )
            ) ForeignKeys
            ON  (ForeignKeys.referenced_object_id = sys.objects.[object_id]) AND (ForeignKeys.referenced_column_id = sys.columns.column_id)
WHERE  (sys.objects.[type] = 'U') AND (sys.objects.[name] NOT IN ('sysdiagrams'))
*/